```html
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>深入理解Java双亲委派机制</title>
    <link href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
    <link href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            line-height: 1.8;
            color: #333;
        }
        h1, h2, h3, h4, h5, h6 {
            font-family: 'Noto Serif SC', serif;
            font-weight: 600;
            color: #222;
        }
        .hero {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        }
        .code-block {
            background-color: #f6f8fa;
            border-radius: 6px;
            padding: 16px;
            font-family: 'SFMono-Regular', Consolas, 'Liberation Mono', Menlo, monospace;
            font-size: 14px;
            line-height: 1.5;
            overflow-x: auto;
        }
        .card {
            transition: transform 0.3s ease, box-shadow 0.3s ease;
        }
        .card:hover {
            transform: translateY(-5px);
            box-shadow: 0 10px 25px rgba(0, 0, 0, 0.1);
        }
        .highlight {
            background-color: rgba(255, 229, 100, 0.3);
            padding: 0.2em 0.4em;
            border-radius: 3px;
        }
        .first-letter {
            font-size: 3.5em;
            float: left;
            line-height: 0.8;
            margin-right: 8px;
            margin-top: 5px;
            color: #667eea;
            font-weight: 700;
        }
    </style>
</head>
<body class="bg-gray-50">
    <!-- Hero Section -->
    <section class="hero text-white py-20 px-4 md:px-0">
        <div class="container mx-auto max-w-5xl text-center">
            <h1 class="text-4xl md:text-5xl font-bold mb-6">深入理解Java双亲委派机制</h1>
            <p class="text-xl md:text-2xl mb-8 opacity-90">探索JVM类加载的核心原理与实践应用</p>
            <div class="flex justify-center space-x-4">
                <a href="#what-is" class="px-6 py-3 bg-white text-indigo-600 font-medium rounded-full hover:bg-gray-100 transition">什么是双亲委派</a>
                <a href="#why" class="px-6 py-3 bg-indigo-700 text-white font-medium rounded-full hover:bg-indigo-800 transition">为什么需要</a>
                <a href="#how" class="px-6 py-3 bg-indigo-700 text-white font-medium rounded-full hover:bg-indigo-800 transition">如何打破</a>
            </div>
        </div>
    </section>

    <!-- Main Content -->
    <main class="container mx-auto max-w-5xl px-4 py-12">
        <!-- What is Section -->
        <section id="what-is" class="mb-20">
            <div class="flex items-center mb-8">
                <i class="fas fa-question-circle text-3xl text-indigo-600 mr-4"></i>
                <h2 class="text-3xl font-bold">什么是双亲委派机制</h2>
            </div>
            
            <div class="flex flex-col md:flex-row gap-8">
                <div class="md:w-2/3">
                    <p class="mb-6 text-lg">
                        <span class="first-letter">双</span>亲委派机制是指在Java虚拟机中，当一个类需要加载时，会先委托其父类加载器加载，如果父类加载器无法加载则会继续向上委托，直到最终委托给Bootstrap ClassLoader，如果Bootstrap ClassLoader也无法加载，则会返回给子类加载器进行加载。这样做的好处是可以保证Java程序的安全性和稳定性，避免不同类加载器加载同一份字节码文件，从而保证了Java程序的隔离性和独立性。
                    </p>
                    
                    <div class="bg-white rounded-xl shadow-md p-6 mb-8 card">
                        <h3 class="text-xl font-semibold mb-4 text-indigo-600"><i class="fas fa-code mr-2"></i>双亲委派实现代码</h3>
                        <p class="mb-4">双亲委派模型的实现主要集中在<code class="highlight">java.lang.ClassLoader</code>的<code class="highlight">loadClass()</code>方法中：</p>
                        <div class="code-block">
                            <pre>protected Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundException {
    synchronized (getClassLoadingLock(name)) {
        // 首先检查类是否已加载
        Class<?> c = findLoadedClass(name);
        if (c == null) {
            try {
                if (parent != null) {
                    // 委托给父加载器加载
                    c = parent.loadClass(name, false);
                } else {
                    // 没有父加载器则委托给启动类加载器
                    c = findBootstrapClassOrNull(name);
                }
            } catch (ClassNotFoundException e) {
                // 父加载器无法加载
            }

            if (c == null) {
                // 自己尝试加载
                c = findClass(name);
            }
        }
        if (resolve) {
            resolveClass(c);
        }
        return c;
    }
}</pre>
                        </div>
                    </div>
                </div>
                
                <div class="md:w-1/3">
                    <div class="bg-white rounded-xl shadow-md p-6 card">
                        <h3 class="text-xl font-semibold mb-4 text-indigo-600"><i class="fas fa-sitemap mr-2"></i>类加载器层次结构</h3>
                        <div class="mermaid">
                            classDiagram
                                direction BT
                                class BootstrapClassLoader
                                class ExtensionClassLoader
                                class ApplicationClassLoader
                                class CustomClassLoader
                                
                                CustomClassLoader --> ApplicationClassLoader : 父加载器
                                ApplicationClassLoader --> ExtensionClassLoader : 父加载器
                                ExtensionClassLoader --> BootstrapClassLoader : 父加载器
                        </div>
                    </div>
                    
                    <div class="bg-white rounded-xl shadow-md p-6 mt-6 card">
                        <h3 class="text-xl font-semibold mb-4 text-indigo-600"><i class="fas fa-info-circle mr-2"></i>关键点</h3>
                        <ul class="space-y-3">
                            <li class="flex items-start">
                                <i class="fas fa-check-circle text-green-500 mt-1 mr-2"></i>
                                <span>加载请求首先委托给父加载器</span>
                            </li>
                            <li class="flex items-start">
                                <i class="fas fa-check-circle text-green-500 mt-1 mr-2"></i>
                                <span>父加载器无法完成加载时才会尝试自己加载</span>
                            </li>
                            <li class="flex items-start">
                                <i class="fas fa-check-circle text-green-500 mt-1 mr-2"></i>
                                <span>保证核心类库由Bootstrap加载器加载</span>
                            </li>
                        </ul>
                    </div>
                </div>
            </div>
        </section>

        <!-- Why Section -->
        <section id="why" class="mb-20">
            <div class="flex items-center mb-8">
                <i class="fas fa-lightbulb text-3xl text-indigo-600 mr-4"></i>
                <h2 class="text-3xl font-bold">为什么需要双亲委派模型</h2>
            </div>
            
            <div class="grid md:grid-cols-2 gap-8">
                <div class="bg-white rounded-xl shadow-md p-6 card">
                    <h3 class="text-xl font-semibold mb-4 text-indigo-600"><i class="fas fa-shield-alt mr-2"></i>安全性</h3>
                    <p class="mb-4">双亲委派模型确保了Java核心类库不会被用户自定义的类所替代。例如<code class="highlight">java.lang.Object</code>类总是由Bootstrap类加载器加载，防止被恶意替换。</p>
                    <img src="https://cdn.nlark.com/yuque/0/2024/png/21449790/1706929750747-505772f7-f67c-4425-aafa-9ee1c1922225.png" alt="双亲委派安全性" class="rounded-lg w-full">
                </div>
                
                <div class="bg-white rounded-xl shadow-md p-6 card">
                    <h3 class="text-xl font-semibold mb-4 text-indigo-600"><i class="fas fa-project-diagram mr-2"></i>一致性</h3>
                    <p class="mb-4">通过委派机制，Java类与其加载器共同形成了一种带有优先级的层次关系，保证了同一个类在不同加载器环境中都是同一个类。</p>
                    <div class="mermaid">
                        flowchart TD
                            A[用户自定义类加载器] --> B[ApplicationClassLoader]
                            B --> C[ExtensionClassLoader]
                            C --> D[BootstrapClassLoader]
                    </div>
                </div>
            </div>
        </section>

        <!-- How Section -->
        <section id="how" class="mb-20">
            <div class="flex items-center mb-8">
                <i class="fas fa-unlock text-3xl text-indigo-600 mr-4"></i>
                <h2 class="text-3xl font-bold">如何打破双亲委派机制</h2>
            </div>
            
            <div class="bg-white rounded-xl shadow-md p-6 mb-8 card">
                <h3 class="text-xl font-semibold mb-4 text-indigo-600"><i class="fas fa-server mr-2"></i>Tomcat的类加载体系</h3>
                <p class="mb-4">Tomcat作为Web服务器需要支持多个Web应用部署，每个Web应用可能需要不同版本的相同类库。传统的双亲委派机制无法满足这一需求。</p>
                <img src="https://cdn.nlark.com/yuque/0/2022/png/617118/1668444139743-5c5d2e2c-1339-40d8-938d-e5b7ccdc10b1.png" alt="Tomcat类加载器" class="rounded-lg w-full mb-4">
                <ul class="space-y-2">
                    <li><strong>CommonClassLoader</strong>: 加载Tomcat和所有Web应用共享的类</li>
                    <li><strong>WebAppClassLoader</strong>: 每个Web应用独有，加载应用特定的类</li>
                    <li><strong>JspClassLoader</strong>: 每个JSP页面独有，实现JSP热部署</li>
                </ul>
            </div>
            
            <div class="grid md:grid-cols-2 gap-8">
                <div class="bg-white rounded-xl shadow-md p-6 card">
                    <h3 class="text-xl font-semibold mb-4 text-indigo-600"><i class="fas fa-code mr-2"></i>不打破双亲委派</h3>
                    <p class="mb-4">只需重写<code class="highlight">findClass()</code>方法，无法被父类加载器加载的类最终会通过这个方法被加载。</p>
                    <div class="code-block">
                        <pre>public class CustomClassLoader extends ClassLoader {
    @Override
    protected Class<?> findClass(String name) throws ClassNotFoundException {
        // 自定义类加载逻辑
        byte[] classData = loadClassData(name);
        return defineClass(name, classData, 0, classData.length);
    }
}</pre>
                    </div>
                </div>
                
                <div class="bg-white rounded-xl shadow-md p-6 card">
                    <h3 class="text-xl font-semibold mb-4 text-indigo-600"><i class="fas fa-code-branch mr-2"></i>打破双亲委派</h3>
                    <p class="mb-4">需要重写<code class="highlight">loadClass()</code>方法，改变默认的委派逻辑。</p>
                    <div class="code-block">
                        <pre>public class CustomClassLoader extends ClassLoader {
    @Override
    public Class<?> loadClass(String name) throws ClassNotFoundException {
        // 直接加载，不委托给父加载器
        byte[] classData = loadClassData(name);
        return defineClass(name, classData, 0, classData.length);
    }
}</pre>
                    </div>
                </div>
            </div>
        </section>

        <!-- Custom ClassLoader Section -->
        <section class="mb-20">
            <div class="flex items-center mb-8">
                <i class="fas fa-cogs text-3xl text-indigo-600 mr-4"></i>
                <h2 class="text-3xl font-bold">自定义类加载器实践</h2>
            </div>
            
            <div class="bg-white rounded-xl shadow-md p-6 mb-8 card">
                <h3 class="text-xl font-semibold mb-4 text-indigo-600"><i class="fas fa-file-code mr-2"></i>自定义类加载器示例</h3>
                <div class="code-block">
                    <pre>public class CustomClassLoader extends ClassLoader {
    private String path = "C:\\Users\\toString\\Desktop\\myLaoder";

    public Class<?> findClass(String name) throws ClassNotFoundException {
        try {
            byte[] classData = loadClassData(name);
            return defineClass(name, classData, 0, classData.length);
        } catch (IOException e) {
            throw new ClassNotFoundException("Class not found: " + name, e);
        }
    }

    private byte[] loadClassData(String className) throws IOException {
        String s = className.replaceAll("\\.", "/");
        File file = new File(path + File.separator + s + ".class");
        FileInputStream fis = new FileInputStream(file);
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        byte[] buffer = new byte[1024];
        int bytesRead;
        while ((bytesRead = fis.read(buffer)) != -1) {
            bos.write(buffer, 0, bytesRead);
        }
        fis.close();
        return bos.toByteArray();
    }
}</pre>
                </div>
            </div>
            
            <div class="grid md:grid-cols-2 gap-8">
                <div class="bg-white rounded-xl shadow-md p-6 card">
                    <h3 class="text-xl font-semibold mb-4 text-indigo-600"><i class="fas fa-exclamation-triangle mr-2"></i>安全限制</h3>
                    <p>尝试加载<code class="highlight">java.lang.String</code>时会遇到安全限制，防止核心类被替换：</p>
                    <img src="https://cdn.nlark.com/yuque/0/2023/png/21449790/1689845162611-d73abd11-bfe4-4968-b7a5-7a379c74e752.png" alt="安全限制" class="rounded-lg w-full mt-4">
                    <p class="mt-4 text-sm text-gray-600">"Prohibited package name: java.lang"是Java的安全机制，禁止开发者使用"java"或"javax"作为包名。</p>
                </div>
                
                <div class="bg-white rounded-xl shadow-md p-6 card">
                    <h3 class="text-xl font-semibold mb-4 text-indigo-600"><i class="fas fa-tasks mr-2"></i>注意事项</h3>
                    <ul class="space-y-3">
                        <li class="flex items-start">
                            <i class="fas fa-check-circle text-green-500 mt-1 mr-2"></i>
                            <span>必须继承<code class="highlight">ClassLoader</code>类并重写<code class="highlight">findClass</code>方法</span>
                        </li>
                        <li class="flex items-start">
                            <i class="fas fa-check-circle text-green-500 mt-1 mr-2"></i>
                            <span>通过<code class="highlight">defineClass</code>将字节码转换为<code class="highlight">Class</code>对象</span>
                        </li>
                        <li class="flex items-start">
                            <i class="fas fa-check-circle text-green-500 mt-1 mr-2"></i>
                            <span>可以实现从非标准位置加载类文件</span>
                        </li>
                        <li class="flex items-start">
                            <i class="fas fa-check-circle text-green-500 mt-1 mr-2"></i>
                            <span>可实现类的版本隔离和热部署</span>
                        </li>
                    </ul>
                </div>
            </div>
        </section>
    </main>

    <!-- Footer -->
    <footer class="bg-gray-900 text-white py-10">
        <div class="container mx-auto max-w-5xl px-4 text-center">
            <div class="mb-4">
                <p class="text-lg font-medium">技术小馆</p>
                <a href="http://www.yuque.com/jtostring" class="text-indigo-300 hover:text-indigo-100 transition">http://www.yuque.com/jtostring</a>
            </div>
            <div class="text-gray-400 text-sm">
                <p>© 2023 深入理解Java双亲委派机制. All rights reserved.</p>
            </div>
        </div>
    </footer>

    <script>
        mermaid.initialize({
            startOnLoad: true,
            theme: 'default',
            flowchart: {
                useMaxWidth: true,
                htmlLabels: true,
                curve: 'basis'
            }
        });
        
        // Smooth scrolling
        document.querySelectorAll('a[href^="#"]').forEach(anchor => {
            anchor.addEventListener('click', function (e) {
                e.preventDefault();
                document.querySelector(this.getAttribute('href')).scrollIntoView({
                    behavior: 'smooth'
                });
            });
        });
    </script>
</body>
</html>
```